package com.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dao.LogDao;
import com.dao.Users;
import com.util.MySqlSessionFactory;

@Controller
public class UsersController {
	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			@RequestParam(value = "password2", defaultValue = "") String password2) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		LogDao logdao = sql.getMapper(LogDao.class);
		Map<String, Object> result = dao.query(account);
		if (!password.equals(password2)) {
			return "ERROR_PASSWORD2";
		}
		if ("1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		dao.add(account, password, "2012-08-02");
		Map<String, Object> map = dao.lastId();
		logdao.add(map.get("id").toString(), "0", "注册");
		sql.commit();
		return "OK";
	}

	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		LogDao logdao = sql.getMapper(LogDao.class);
		Map<String, Object> result = dao.queryone(account, password);
		if ("0".equals(result.get("counter").toString())) {
			return "ERR";
		}
		logdao.add(result.get("id").toString(), "0", "登陆");
		response.addCookie(new Cookie("user", result.get("id").toString()));
		sql.commit();
		return "OK";
	}

	@RequestMapping(value = "/queryUsers.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryUsers(
			@CookieValue(value = "user", defaultValue = "") String userid,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		Map<String, Object> result = dao.queryUsers(userid);
		sql.commit();
		return result;
	}

	@RequestMapping(value = "/queryday.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> queryday(
			@RequestParam(value = "date_time", defaultValue = "") String date_time) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> map = dao.queryday(date_time);
		return map;
	}

	@RequestMapping(value = "/queryin.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryin(
			@CookieValue(value = "user", defaultValue = "") String userid,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		Map<String, Object> map = dao.queryin(userid);
		return map;
	}

	@RequestMapping(value = "/queryout.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryout(
			@CookieValue(value = "user", defaultValue = "") String userid,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		Map<String, Object> map = dao.queryout(userid);
		return map;
	}

	@RequestMapping(value = "/queryjin.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> queryjin(
			@CookieValue(value = "user", defaultValue = "") String userid,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> map = dao.queryjin(userid);
		return map;
	}

	@RequestMapping(value = "/queryjout.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> queryjout(
			@CookieValue(value = "user", defaultValue = "") String userid,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> map = dao.queryjout(userid);
		return map;
	}

	@RequestMapping(value = "/shouru.action", method = RequestMethod.POST)
	@ResponseBody
	public String shouru(
			@CookieValue(value = "user", defaultValue = "") String user_id,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		dao.addin(user_id, date_time, place, money_in, kind, remark);
		LogDao logdao = sql.getMapper(LogDao.class);
		Map<String, Object> map = dao.lastId();
		logdao.add(user_id, map.get("id").toString(), "收入");
		sql.commit();
		return "OK";
	}

	@RequestMapping(value = "/zhichu.action", method = RequestMethod.POST)
	@ResponseBody
	public String zhichu(
			@CookieValue(value = "user", defaultValue = "") String user_id,
			HttpServletResponse response,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		dao.addout(user_id, date_time, place, money_out, kind, remark);
		LogDao logdao = sql.getMapper(LogDao.class);
		Map<String, Object> map = dao.lastId();
		logdao.add(user_id, map.get("id").toString(), "支出");
		sql.commit();
		return "OK";
	}

	@RequestMapping(value = "/queryz.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> queryz(
			@CookieValue(value = "id", defaultValue = "") String id,
			@CookieValue(value = "user", defaultValue = "") String userid,
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> map = dao.queryz(id, userid, date_time,
				place, money_in, money_out, kind, remark);
		// System.out.println(userid +" "+ date_time +" "+ place +" "+ money_in
		// +" "+ money_out +" "+ kind +" "+ remark);
		// System.out.println(map);
		return map;
	}

	@RequestMapping(value = "/querynull.action", method = RequestMethod.POST)
	@ResponseBody
	public String querynull(
			@CookieValue(value = "user", defaultValue = "") String userid,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		LogDao logdao = sql.getMapper(LogDao.class);
		Map<String, Object> map = dao.lastId();
		logdao.add(userid, map.get("id").toString(), "退出登录");
		response.addCookie(new Cookie("user", null));
		// System.out.println(result);
		sql.commit();
		return "OK";
	}

	@RequestMapping(value = "/queryall.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> queryall(
			@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "user", defaultValue = "") String userid,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> map = dao.queryall(id, userid);
		// System.out.println(userid +" "+ date_time +" "+ place +" "+ money_in
		// +" "+ money_out +" "+ kind +" "+ remark);
		// System.out.println(map);
		return map;
	}

	@RequestMapping(value = "/del.action", method = RequestMethod.POST)
	@ResponseBody
	public String del(
			@CookieValue(value = "user", defaultValue = "") String userid,
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		SqlSession sql = MySqlSessionFactory.getSqlSession();
		Users dao = sql.getMapper(Users.class);
		dao.del(id, userid);
		LogDao logdao = sql.getMapper(LogDao.class);
		logdao.add(userid, id, "删除");
		sql.commit();
		// System.out.println(id);
		// System.out.println(userid +" "+ date_time +" "+ place +" "+ money_in
		// +" "+ money_out +" "+ kind +" "+ remark);
		// System.out.println(map);
		return "OK";
	}

}
